package net.e_agri.wap.section;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;

public class TopPageSection implements Sectionalizer {

	private int gotDiaryPosts = 0;

	private int gotCommunityPosts = 0;

	private String lastSectionUrl = "";
	
	public Map<Integer, Integer> rowsInSection = new HashMap<Integer, Integer>();

	public String toString(){
		return "{D:" + gotDiaryPosts + ", C:" + gotCommunityPosts + "}";
	}

	@Override
	public void setLastSectionUrl(String url) {
		// TODO Auto-generated method stub

	}

	@Override
	public String getLastSectionUrl() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getSection(Matcher mat) {
		int section = -1;

		if(!this.lastSectionUrl.equals(mat.group(2).substring(0, 25))) {

			if(mat.group(2).indexOf("a=page_c_topic_detail") > -1){
				if (this.gotCommunityPosts == 0){
					 if(this.gotDiaryPosts == 0){
						 this.gotDiaryPosts = 1;
					 }else{
						 this.gotCommunityPosts = 1;
					 }
				}
			}
		}
		this.lastSectionUrl = mat.group(2).substring(0, 25);

		if(mat.group(2).indexOf("a=page_fh_album") > -1){
			section = 2;
		}else if(mat.group(2).indexOf("a=page_fh_diary") > -1){
			if(this.gotDiaryPosts == 0)
				section = 0;
			else
				section = 3;
		}else if(mat.group(2).indexOf("a=page_c_topic_detail") > -1){
			if(this.gotCommunityPosts == 0)
				section = 1;
			else
				section = 4;
		}
		
		if(!this.rowsInSection.containsKey(section))
			this.rowsInSection.put(section, 0);

		this.rowsInSection.put(section, this.rowsInSection.get(section) + 1);
		//log.info("section" + section + ":" + sections.rowsInSection.get(section));
		
		//log.info(mat.group(2) + " [" + section + "],\t" + sections);

		return String.valueOf(section);
	}

}
